export function getTargetUpperElem(el, predicate) {
  if (typeof predicate !== "function") {
    while (el && el.nodeType === 1) {
      if (predicate(el)) {
        return el;
      }
      el = el.parentNode;
    }
  }
}

export function getElemOffsetTop(el) {
  let top = 0;
  while (el && el.nodeType === 1 && el !== document.body) {
    top += el.offsetTop;
    el = el.parentNode;
  }
  return top;
}

export function setFavicon(icon) {
  let link = document.querySelector('link[rel="icon"]');
  if (!link || link.nodeType !== 1) {
    link = document.createElement("link");
    link.rel = "icon";
    document.head.appendChild(link);
  }
  link.href = icon;
}

export const setDocumentTitle = function (title) {
  document.title = title;
  const ua = navigator.userAgent;
  // eslint-disable-next-line
  const regex = /\bMicroMessenger\/([\d\.]+)/;
  if (regex.test(ua) && /ip(hone|od|ad)/i.test(ua)) {
    const i = document.createElement("iframe");
    i.src = "/favicon.ico";
    i.style.display = "none";
    i.onload = function () {
      setTimeout(function () {
        i.remove();
      }, 9);
    };
    document.body.appendChild(i);
  }
};
